#include "stdafx.h"
#include "SpriteSheetAnimation.h"
#include "SDLTexture.h"
#include <iostream>
#include <fstream>

using namespace std;

namespace ZeEngine
{

    SpriteSheetAnimation::SpriteSheetAnimation(const string& strName, bool reverse, const vector<SDL_Rect>& frames, const vector<s32>& frameTimes) : 
                                                                                                        m_name(strName),
                                                                                                        m_reverse(reverse), 
                                                                                                        m_frames(frames), 
                                                                                                        m_frameTimes(frameTimes)
    {
        if (m_frames.size() == m_frameTimes.size())
        {
            m_numFrames = m_frames.size();
        }
        else
        {
            LOG_WARNING("Ensure that the animation " + m_name + " is properly defined and that it has the right number of frames and frame durations");
        }
    }

    SpriteSheetAnimation::~SpriteSheetAnimation()
    {
    }

    const SDL_Rect SpriteSheetAnimation::GetFrameBoundary(u32 frame) const
    {
        SDL_Rect rect = {0, 0, 0 ,0};

        if (frame < m_frames.size()  )
        {
            rect = m_frames[frame];
        }
        else
        {
            stringstream ss; 
            ss << frame;
            LOG_WARNING("Could not find frame " + ss.str() + " in sprite sheet animation " + m_name);
        }

        return rect;
    }

    const s32 SpriteSheetAnimation::GetFrameDuration(u32 frame) const
    {
        s32 frameDuration = 0;

        if (frame < m_frameTimes.size())
        {
            frameDuration = m_frameTimes[frame];
        }
        else
        {
            stringstream ss; 
            ss << frame;
            LOG_WARNING("Could not find frame " + ss.str() + " in sprite sheet animation " + m_name);
        }

        return frameDuration;
    }
}